package com.golden.procedure.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.golden.procedure.client.DicClientAPI;
import com.golden.procedure.client.EmployeeClientAPI;
import com.golden.procedure.client.OrgClientApi;
import com.golden.procedure.clientDO.DictionariesDO;
import com.golden.procedure.clientDO.EmployeeDO;
import com.golden.procedure.clientDO.OrgDO;
import com.golden.procedure.domain.OrderProductDO;
import com.golden.procedure.domain.SiteDO;
import com.golden.procedure.domain.WorkAttachmentDO;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.domain.WorkDesignOrderDO;
import com.golden.procedure.domain.WorkOrderBookDO;
import com.golden.procedure.domain.WorkOrderDO;
import com.golden.procedure.domain.WorkOrderPhotographyDO;
import com.golden.procedure.domain.WorkOrderPickDO;
import com.golden.procedure.domain.WorkSeatSchedulingDO;
import com.golden.procedure.domain.WorkSiteSeatDO;
import com.golden.procedure.domain.WorkerDO;
import com.golden.procedure.domain.WorkerRoleDO;
import com.golden.procedure.entity.SysSite;
import com.golden.procedure.entity.WorkAttachment;
import com.golden.procedure.entity.WorkComment;
import com.golden.procedure.entity.WorkDesignOrder;
import com.golden.procedure.entity.WorkOrder;
import com.golden.procedure.entity.WorkOrderBook;
import com.golden.procedure.entity.WorkOrderPhotography;
import com.golden.procedure.entity.WorkOrderPick;
import com.golden.procedure.entity.WorkSeatScheduling;
import com.golden.procedure.entity.WorkSiteSeat;
import com.golden.procedure.entity.Worker;
import com.golden.procedure.entity.WorkerRole;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;
import com.golden.procedure.util.TaskDefKey;

/**
 * @author reagan
 * @Description:基础服务类主要用来实现底层的通用方法
 */
public class BaseService {

	@Autowired
	EmployeeClientAPI employeeClientAPI;
	@Autowired
	OrgClientApi orgClientImpl;
	@Autowired
	DicClientAPI dicClientAPI;

	// TODO： 员工接口（临时方法）
	public EmployeeDO getEmployee(Integer id) {
		try {
			EmployeeDO employeeDO = employeeClientAPI.get(id);
			if (employeeDO != null) {
				return employeeDO;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		EmployeeDO employee = new EmployeeDO();
		employee.setId(id);
		employee.setRealName("测试人员");
		employee.setOrgId(1);
		employee.setDepartment("系统管理小组");
		employee.setJobsId(1);
		employee.setJobName("高级系统管理员");
		employee.setMobile("13800138000");

		return employee;
	}

	/**
	 * 判断整型数据是否无效： null或<1为无效,返回true; >=1为有效，返回false
	 */
	public static boolean isNumberInValid(Integer value) {
		if (value == null) {
			return true;
		} else if (value < 1) {
			return true;
		}
		return false;
	}

	public SiteDO getSiteDO(SysSite obj) {
		if (obj == null)
			return null;
		SiteDO dst = new SiteDO();
		return BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
	}

	public List<SiteDO> getSiteDOList(List<SysSite> list) {
		List<SiteDO> result = new ArrayList<SiteDO>();
		if (list != null && !list.isEmpty()) {
			for (SysSite org : list) {
				SiteDO org1 = getSiteDO(org);
				if (org1 != null) {
					result.add(org1);
				}
			}
		}
		return result;
	}

	public WorkerDO getWorkerDO(Worker obj) {
		if (obj == null)
			return null;
		WorkerDO dst = new WorkerDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setModifitime(obj.getModifitime().getTime() + "");
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		dst.setSchedulingDateStr(obj.getSchedulingDate().getTime() + "");
		return dst;
	}

	public List<WorkerDO> getWorkerDOList(List<Worker> list) {
		List<WorkerDO> doList = new ArrayList<WorkerDO>();
		if (null != list && list.size() > 0) {
			for (Worker data : list) {
				WorkerDO dst = getWorkerDO(data);
				doList.add(dst);
			}
		}
		return doList;
	}

	public List<WorkerRoleDO> getWorkerRoleDOList(List<WorkerRole> list) {
		List<WorkerRoleDO> doList = new ArrayList<WorkerRoleDO>();
		for (WorkerRole data : list) {
			WorkerRoleDO dst = new WorkerRoleDO();
			if (BeanUtilsExtends.copyProperties(dst, data)) {
				doList.add(dst);
			}
		}
		return doList;
	}

	public WorkSiteSeatDO getWorkSiteSeatDO(WorkSiteSeat obj) {
		if (obj == null)
			return null;
		WorkSiteSeatDO dst = new WorkSiteSeatDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setModifitime(obj.getModifitime().getTime() + "");
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		dst.setSchedulingDateStr(obj.getSchedulingDate().getTime() + "");
		// 查询服务等级(字典)
		if (!isNumberInValid(obj.getServiceClass())) {
			// TODO: 服务接口调用
//			DictionariesDO dic = dicClientAPI.get(obj.getServiceClass());
//			dst.setServiceClassName(dic.getDataItem());
			dst.setServiceClassName("七星级");
		}
		return dst;
	}

	public List<WorkSeatSchedulingDO> getWorkSeatSchedulingDOList(List<WorkSeatScheduling> list) {
		if (null == list || list.isEmpty())
			return null;
		List<WorkSeatSchedulingDO> doList = new ArrayList<WorkSeatSchedulingDO>();
		for (WorkSeatScheduling data : list) {
			WorkSeatSchedulingDO dst = getWorkSeatSchedulingDO(data);
			doList.add(dst);
		}
		return doList;
	}

	public WorkSeatSchedulingDO getWorkSeatSchedulingDO(WorkSeatScheduling obj) {
		if (obj == null)
			return null;
		WorkSeatSchedulingDO dst = new WorkSeatSchedulingDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		dst.setModifitime(obj.getModifitime().getTime() + "");
		dst.setSchedulingDateStr(obj.getSchedulingDate().getTime() + "");
		dst.setPlanStartOnStr(new SimpleDateFormat("HH:mm").format(obj.getPlanStartOn()));
		if (dst.getStatus() == BonConstant.SEAT_STATUS_USED) {
			dst.setStatusName("占用");
		} else if (dst.getStatus() == BonConstant.SEAT_STATUS_ENABLE) {
			dst.setStatusName("可用");
		} else if (dst.getStatus() == BonConstant.SEAT_STATUS_DISABLE) {
			dst.setStatusName("禁用");
		}
		return dst;
	}

	public WorkOrderDO getWorkOrderDO(WorkOrder obj) {
		if (obj == null)
			return null;
		WorkOrderDO dst = new WorkOrderDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setModifitime(obj.getModifitime().getTime() + "");
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		// 当前节点
		if (null != obj.getTaskDefID()) {
			dst.setTaskDefName(TaskDefKey.getNameByKey(obj.getTaskDefID()));
		}
		// 改期日期
		if (null != obj.getChangeDate()) {
			dst.setChangeDateStr(obj.getChangeDate().getTime() + "");
		}
		// 计划拍摄日期
		if (null != obj.getPlanShotDate()) {
			dst.setPlanShotDateStr(obj.getPlanShotDate().getTime() + "");
		}
		// TODO：订单类型
		// DictionariesDO dic = dicClientAPI.get(obj.getOrderType());
		// dst.setOrderTypeStr(dic.getDataItem());
		dst.setOrderTypeStr("免费照");
		// 订单状态 0-正常; 2-重拍订单; 3-补拍订单; 4-改期订单; -1取消
		switch (obj.getOrderStatus()) {
		case BonConstant.ORDER_STATUS_NORMAL:
			dst.setOrderStatusName("正常");
			break;
		case BonConstant.ORDER_STATUS_CHANGEDATE:
			dst.setOrderStatusName("改期");
			break;
		case BonConstant.ORDER_STATUS_REPHOTOGRAPH:
			dst.setOrderStatusName("重拍");
			break;
		case BonConstant.ORDER_STATUS_RESHOT:
			dst.setOrderStatusName("补拍");
			break;
		case BonConstant.ORDER_STATUS_CANCEL:
			dst.setOrderStatusName("取消");
			break;
		default:
			dst.setOrderStatusName("");
			break;
		}
		return dst;
	}

	public List<WorkOrderDO> getWorkOrderDOList(List<WorkOrder> list) {
		List<WorkOrderDO> doList = new ArrayList<WorkOrderDO>();
		if (null != list && list.size() > 0) {
			for (WorkOrder data : list) {
				WorkOrderDO dst = getWorkOrderDO(data);
				doList.add(dst);
			}
		}
		return doList;
	}

	public WorkOrderBookDO getWorkOrderBookDO(WorkOrderBook obj) {
		if (obj == null)
			return null;
		WorkOrderBookDO dst = new WorkOrderBookDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setModifitime(obj.getModifitime().getTime() + "");
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		if (null != obj.getSchedulingDate()) {
			dst.setSchedulingDateStr(obj.getSchedulingDate().getTime() + "");
		}
		return dst;
	}

	public List<WorkOrderBookDO> getWorkOrderYYDOList(List<WorkOrderBook> list) {
		List<WorkOrderBookDO> doList = new ArrayList<WorkOrderBookDO>();
		if (null != list && list.size() > 0) {
			for (WorkOrderBook data : list) {
				WorkOrderBookDO dst = getWorkOrderBookDO(data);
				doList.add(dst);
			}
		}
		return doList;
	}

	public List<WorkCommentDO> getWorkCommentDOList(List<WorkComment> list) {
		List<WorkCommentDO> doList = new ArrayList<WorkCommentDO>();
		if (null != list && list.size() > 0) {
			for (WorkComment data : list) {
				WorkCommentDO dst = new WorkCommentDO();
				if (BeanUtilsExtends.copyProperties(dst, data)) {
					dst.setModifitime(data.getModifitime().getTime() + "");
					dst.setCreatetime(data.getCreatetime().getTime() + "");
					doList.add(dst);
				}
			}
		}
		return doList;
	}

	public WorkOrderPickDO getWOrkOrderPickDO(WorkOrderPick obj) {
		if (obj == null)
			return null;
		WorkOrderPickDO dst = new WorkOrderPickDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		// 计划选片时间
		dst.setPlanPickDateStr(obj.getPlanPickDate().getTime() + "");
		// 实际选片时间
		if (null != obj.getPickDate()) {
			dst.setPickDateStr(obj.getPickDate().getTime() + "");
		}
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		if (null != obj.getModifitime()) {
			dst.setModifitime(obj.getModifitime().getTime() + "");
		}
		// 当前任务节点
		dst.setTaskName(TaskDefKey.getNameByKey(dst.getTaskDefID()));
		// 状态
		if (obj.getStatus() == BonConstant.PICK_STATUS_WAITING) {
			dst.setStatusName("未开始");
		} else if (obj.getStatus() == BonConstant.PICK_STATUS_ING) {
			dst.setStatusName("进行中");
		} else if (obj.getStatus() == BonConstant.PICK_STATUS_OK) {
			dst.setStatusName("已完成");
		} else if (obj.getStatus() == BonConstant.PICK_STATUS_CANCLE) {
			dst.setStatusName("已取消");
		} else {
			dst.setStatusName("重拍/补拍/改期");
		}
		return dst;
	}

	public List<WorkAttachmentDO> getWorkAttachmentDOList(List<WorkAttachment> list) {
		List<WorkAttachmentDO> dstList = new ArrayList<WorkAttachmentDO>();
		for (WorkAttachment data : list) {
			WorkAttachmentDO dataDO = getWorkAttachmentDO(data);
			if (dataDO != null) {
				dstList.add(dataDO);
			}
		}
		return dstList;
	}

	public WorkAttachmentDO getWorkAttachmentDO(WorkAttachment obj) {
		if (obj == null)
			return null;
		WorkAttachmentDO dst = new WorkAttachmentDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		if (null != obj.getModifitime()) {
			dst.setModifitime(obj.getModifitime().getTime() + "");
		}
		return dst;
	}

	public WorkDesignOrderDO getWorkDesignOrderDO(WorkDesignOrder obj) {
		if (obj == null)
			return null;
		WorkDesignOrderDO dst = new WorkDesignOrderDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		if (null != obj.getCheckDesignDate()) {
			dst.setCheckDesignDateStr(obj.getCheckDesignDate().getTime() + "");
		}
		if (null != obj.getModifitime()) {
			dst.setDeliveryDateStr(obj.getDeliveryDate().getTime() + "");
		}
		if (null != obj.getModifitime()) {
			dst.setModifitime(obj.getModifitime().getTime() + "");
		}
		return dst;
	}

	public WorkOrderPhotographyDO getWorkOrderPhotographyDO(WorkOrderPhotography obj) {
		if (obj == null)
			return null;
		WorkOrderPhotographyDO dst = new WorkOrderPhotographyDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		if (null != obj.getModifitime()) {
			dst.setModifitime(obj.getModifitime().getTime() + "");
		}
		if (null != obj.getPlanDate()) {
			dst.setPlanDateStr(obj.getPlanDate().getTime() + "");
		}
		if (null != obj.getShotDate()) {
			dst.setShotDateStr(obj.getShotDate().getTime() + "");
		}

		// 拍摄状态 0-未拍,1-已拍,2-重拍,3-补拍,4-改期,-1取消
		if (obj.getShotStatus() == 0) {
			dst.setShotStatusName("未拍摄");
		} else if (obj.getShotStatus() == 1) {
			dst.setShotStatusName("已拍摄");
		} else if (obj.getShotStatus() == 2) {
			dst.setShotStatusName("重拍");
		} else if (obj.getShotStatus() == 3) {
			dst.setShotStatusName("补拍");
		} else if (obj.getShotStatus() == 4) {
			dst.setShotStatusName("拍摄改期");
		} else if (obj.getShotStatus() == -1) {
			dst.setShotStatusName("拍摄取消");
		} else {
			dst.setShotStatusName("其他");
		}
		return dst;
	}

	/** TODO: 商品测试数据 */
	public List<OrderProductDO> getProductList(Integer workOrderID, Integer orderID, Integer sceenID) {

		int id = workOrderID * 100;
		List<OrderProductDO> productDOList = new ArrayList<OrderProductDO>();
		Integer[] ids = new Integer[] { id, id + 1, id + 2, id + 3, id + 4, id + 5, id + 6 };
		String[] names = new String[] { "xx相册", "yy相册", "xx相框", "yy相框", "zz相框", "xx摆台", "入底" };
		Integer[] planNums = new Integer[] { 16, 12, 1, 1, 1, 1, 32 };
		Integer[] hasCover = new Integer[] { 1, 1, 0, 0, 0, 0, 0 };
		Integer[] flags = new Integer[] { 0, 0, 0, 0, 0, 0, 1 };
		for (int i = 0; i < ids.length; i++) {
			OrderProductDO data = new OrderProductDO();
			data.setWorkOrderID(workOrderID);
			data.setOrderID(orderID);
			data.setOrderProductSceenID(sceenID);
			data.setOrderProductID(ids[i]);
			data.setOrderProductName(names[i]);
			data.setPlanNum(planNums[i]); // 入册基数
			data.setRdFlag(flags[i]);
			data.setHasCover(hasCover[i]);
			productDOList.add(data);
		}
		return productDOList;
	}

	/** TODO：获取子部门id列表 */
	public List<Integer> getOrgIDs(Integer orgID) {

		// List<Integer> ids = new ArrayList<Integer>();
		// OrgDO orgDO = orgClientImpl.get(orgID);
		// if (null != orgDO) {
		// List<EmployeeDO> list = employeeClientAPI.query("all",
		// orgDO.getCode(), 0);
		// for (EmployeeDO data : list) {
		// if (!ids.contains(data.getOrgId())) {
		// ids.add(data.getOrgId());
		// }
		// }
		// }
		// return ids;
		return Arrays.asList(new Integer[] { 1, 2 });
	}

	/** 获取部门下所有员工id列表 */
	public List<Integer> getEmployeeIDs(Integer orgID) {
		List<Integer> ids = new ArrayList<Integer>();
		OrgDO orgDO = orgClientImpl.get(orgID);
		if (null != orgDO) {
			List<EmployeeDO> list = employeeClientAPI.query("all", orgDO.getCode(), 0, "all");
			for (EmployeeDO data : list) {
				ids.add(data.getId());
			}
		}
		return ids;
	}
}
